#include "newfilewindow.h"
#include "ui_newfilewindow.h"

#include <QtCore>
#include <QtGui>

newFileWindow::newFileWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::newFileWindow)
{
    ui->setupUi(this);
    this->setCentralWidget(ui->textEdit);
}

newFileWindow::~newFileWindow()
{
    delete ui;
}

void newFileWindow::on_actionNew_triggered()
{
    //New
    QString text;
    if(m_newFilename.isEmpty())
    {
        if(!ui->textEdit->toPlainText().isEmpty())
        {
            int a = QMessageBox::question(this,"Save","Do you want to save the file",QMessageBox::Yes,QMessageBox::No);
            if(a==QMessageBox::Yes)
            {
                on_actionSave_triggered();
            }
            else if(a==QMessageBox::No)
            {
                m_newFilename = "";
                ui->textEdit->clear();
            }
        }
    }
    else
    {
        QFile file(m_newFilename);
        if(file.open(QFile::ReadOnly | QFile::Text))
        {
            QTextStream in(&file);
            if(ui->textEdit->toPlainText().compare(in.readAll())==0)
            {
                m_newFilename = "";
                ui->textEdit->clear();
            }
            else
            {
                int a = QMessageBox::question(this,"Save","Do you want to save the file",QMessageBox::Yes,QMessageBox::No);
                if(a==QMessageBox::Yes)
                {
                    on_actionSave_triggered();
                    ui->textEdit->clear();
                }
                else if(a==QMessageBox::No)
                {
                    m_newFilename = "";
                    ui->textEdit->clear();
                }
            }
        }
        file.close();
    }
}

void newFileWindow::on_actionOpen_triggered()
{
    //Open
    QString file = QFileDialog::getOpenFileName(this,"Open File");
    if(!file.isEmpty())
    {
        QFile sFile(file);
        if(sFile.open(QFile::ReadOnly | QFile::Text))
        {
            QTextStream in(&sFile);
            m_newFilename = file;
            QString text = in.readAll();

            sFile.close();

            ui->textEdit->setText(text);
        }
    }
}

void newFileWindow::on_actionSave_triggered()
{
    //Save
    if(m_newFilename.length()!=0)
    {
        QFile sfile(m_newFilename);
        if(sfile.open(QFile::WriteOnly | QFile::Text))
        {
            QTextStream out(&sfile);

            out<<ui->textEdit->toPlainText();
            sfile.flush();
            sfile.close();
        }
    }
    else
        on_actionSave_As_triggered();
}

void newFileWindow::on_actionSave_As_triggered()
{
    //Save As
    QString file = QFileDialog::getSaveFileName(this,"open file");
    if(!file.isEmpty())
    {
        m_newFilename = file;
        on_actionSave_triggered();
    }
}

void newFileWindow::on_actionCut_triggered()
{
    //Cut
    ui->textEdit->cut();
}

void newFileWindow::on_actionPaste_triggered()
{
    //Paste
    ui->textEdit->paste();
}

void newFileWindow::on_actionCopy_triggered()
{
    //Copy
    ui->textEdit->copy();
}

void newFileWindow::on_actionUndo_triggered()
{
    //Undo
    ui->textEdit->undo();
}

void newFileWindow::on_actionRedo_triggered()
{
    //Redo
    ui->textEdit->redo();
}
